<template lang="pug">
  kalix-dialog.user-add(bizKey="user" ref="kalixBizDialog" v-bind:formModel.sync="formModel" v-bind:targetURL="targetURL")
    div.el-form(slot="dialogFormSlot")
      <!--el-form-item(label="用户类型" prop="userType" v-bind:rules="rules.userType" v-bind:label-width="labelWidth")-->
      <!--kalix-dict-select(v-model="formModel.userType" appName="admin" dictType="用户类型" v-on:input="change($event)")-->
      el-form-item(label="登录名" prop="loginName" v-bind:rules="rules.loginName" v-bind:label-width="labelWidth")
        el-input(v-model="formModel.loginName")
        p
          font(color="blue") 注：默认密码为123
      template(v-if="formModel.userType === 0 || formModel.userType === 2")
        el-form-item(label="工号" prop="code" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.code")
        el-form-item(label="岗位" prop="position" v-bind:label-width="labelWidth")
          kalix-dict-select(v-model="formModel.position" appName="admin" dictType="岗位名称" style="width:100%")
        el-form-item(label="姓名" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.name")
        el-form-item(label="性别" prop="sex" v-bind:label-width="labelWidth")
          el-radio-group(v-model="formModel.sex")
            el-radio(label="男")
            el-radio(label="女")
        el-form-item(label="邮箱" prop="email" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.email")
        kalix-phone-form-item(v-model="formModel.phone" v-bind:label-width="labelWidth")
        kalix-mobile-form-item(v-model="formModel.mobile" v-bind:label-width="labelWidth")
      template(v-else-if="formModel.userType === 1")
        el-form-item(label="学号" prop="code" v-bind:rules="rules.code_xh" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.code")
        el-form-item(label="姓名" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.name")
        el-form-item(label="性别" prop="sex" v-bind:label-width="labelWidth")
          el-radio-group(v-model="formModel.sex")
            el-radio(label="男")
            el-radio(label="女")
        el-form-item(label="邮箱" prop="email" v-bind:rules="rules.email" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.email")
        kalix-phone-form-item(v-model="formModel.phone" v-bind:label-width="labelWidth")
        kalix-mobile-form-item(v-model="formModel.mobile" required v-bind:label-width="labelWidth")
      template(v-else-if="formModel.userType === 3")
        el-form-item(label="企业组织机构代码" prop="code" v-bind:rules="rules.code_qy" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.code")
        el-form-item(label="企业名称" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.name")
        el-form-item(label="邮箱" prop="email" v-bind:rules="rules.email" v-bind:label-width="labelWidth")
          el-input(v-model="formModel.email")
        kalix-phone-form-item(v-model="formModel.phone" required v-bind:label-width="labelWidth")
        kalix-mobile-form-item(v-model="formModel.mobile" v-bind:label-width="labelWidth")
      template(v-else)
        // 暂无
      <!--el-form-item(label="头像" prop="icon" v-bind:label-width="labelWidth")-->
        <!--kalix-upload(v-model="formModel.icon" v-bind:isImage="true" style="width:100%")-->
      el-form-item(label="密码" prop="password" v-bind:rules="rules.password" v-bind:label-width="labelWidth")
        el-input(v-model="formModel.password" type="password")
      <!--el-form-item(label="确认密码" prop="confirmPassword" v-bind:rules="rules.confirmPassword" v-bind:label-width="labelWidth")-->
      <!--el-input(v-model="formModel.confirmPassword" type="password")-->
      el-form-item(label="姓名" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
        el-input(v-model="formModel.name")
      el-form-item(label="性别" prop="sex" v-bind:label-width="labelWidth")
        el-radio-group(v-model="formModel.sex")
          el-radio(label="男")
          el-radio(label="女")
      el-form-item(label="邮箱" prop="email" v-bind:label-width="labelWidth")
        el-input(v-model="formModel.email")
      el-form-item(label="手机" prop="mobile"  v-bind:rules="rules.mobile" v-bind:label-width="labelWidth")
        el-input(v-model="formModel.mobile")
      el-form-item(label="简介" prop="intro" v-bind:label-width="labelWidth")
        el-input(v-model="formModel.intro" type="textarea" :rows="3")
      <!--el-form-item(label="状态" prop="available" v-bind:label-width="labelWidth")-->
      <!--el-switch(v-model="formModel.available" active-text="停用" inactive-text="启用" active-value="0" inactive-value="1")-->
</template>
<script type="text/ecmascript-6">
  import FormModel from './model'
  import {userURL} from '../../config.toml'

  export default {
    name: 'AdminUserAdd',
    data() {
      let validateLoginName = (rule, value, callback) => {
        if (value === '' || !value) {
          callback(new Error('请输入登录名'))
        } else if (value.indexOf(' ') >= 0) {
          console.log(value)
          callback(new Error('登录名包含空格'))
        } else {
          this.axios.request({
            method: 'GET',
            url: userURL + '/loginname/' + value,
            data: '',
            params: {}
          }).then(response => {
            if (response.data) {
              callback(new Error('该登录名已被使用'))
            } else {
              callback()
            }
          }).catch(() => {
          })
        }
      }
      return {
        formModel: Object.assign({}, FormModel),
        rules: {
          // userType: [{type: 'number', required: true, message: '请选择用户类别', trigger: 'change'}],
          // code_xh: [{required: true, message: '请输入学生学号', trigger: 'blur'}],
          // code_qy: [{required: true, message: '请输入企业组织机构代码', trigger: 'blur'}],
          name: [{required: true, message: '请输入名称', trigger: 'blur'}],
          // loginName: [{required: true, message: '请输入登录名', trigger: 'blur'}],
          loginName: [{required: true, validator: validateLoginName, trigger: 'blur'},
            {min: 2, max: 8, message: '用户名长度在 2 到 8 个字符', trigger: 'blur'},
            {
              required: true,
              pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
              message: '登录名不支持特殊字符',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
              message: '密码不支持特殊字符',
              trigger: 'blur'
            }
          ],
          mobile: [
            {
              required: true,
              pattern: /^1(3|4|5|6|7|8|9)\d{9}$/,
              message: '手机号格式不正确',
              trigger: 'blur'
            }
          ],
          /* password: [
            {required: true, validator: validatePassword, trigger: 'blur'}
          ],
          confirmPassword: [
            {required: true, validator: validateConfirmPassword, trigger: 'blur'}
          ], */
          email: [
            {required: true, message: '请输入邮箱地址', trigger: 'blur'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}
          ]
        },
        targetURL: userURL,
        labelWidth: '140px'
      }
    },
    created() {
    },
    methods: {
      change(event) {
        this.$refs.kalixBizDialog.$refs.dialogForm.clearValidate()
      }
    }
  }
</script>
